import { createRouter, createWebHistory } from "vue-router";
import Login from "../views/Login.vue";
import Dashboard from "../views/Dashboard.vue";
// import Food from "../views/Food.vue";
import Layout from "../layout/Layout.vue";
import { useAuthStore } from "../stores/userStore";

const routes = [
  {
    path: "/",
    name: "Home",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/dashboard",
    name: "Dashboard",
    component: Layout,
    children: [
      {
        path: "",
        name: "DashboardIndex",
        component: Dashboard,
      },
    ],
  },
  {
    path: "/users/users-setting",
    component: Layout,
    children: [
      {
        path: "",
        name: "UserSetting",
        component: () => import("../views/user/UserSetting.vue"),
        meta: { title: "个人信息", icon: "Van" },
      },
    ],
  },
  {
    path: "/dailyLogs",
    name: "dailyLogs",
    component: Layout,
    children: [
      {
        path: "",
        name: "dailyLogsIndex",
        component: () => import("../views/dailyLogs/index.vue"),
        meta: { title: "经营状况", icon: "Van" },
      },
    ],
  },
  {
    path: "/costStatistics",
    name: "costStatistics",
    component: Layout,
    children: [
      {
        path: "",
        name: "costStatisticsIndex",
        component: () => import("../views/costStatistics/index.vue"),
        meta: { title: "成本统计", icon: "Van" },
      },
    ],
  },
  {
    path: "/goods",
    name: "Goods",
    component: Layout,
    children: [
      {
        path: "",
        name: "GoodsIndex",
        component: () => import("../views/Goods/index.vue"),
        meta: { title: "食材档案", icon: "Goods" },
      },
    ],
  },
  {
    path: "/supplier",
    name: "supplier",
    component: Layout,
    children: [
      {
        path: "",
        name: "supplierIndex",
        component: () => import("../views/supplier/index.vue"),
        meta: { title: "供应商管理", icon: "Van" },
      },
    ],
  },
  {
    path: "/house",
    name: "house",
    component: Layout,
    children: [
      {
        path: "",
        name: "houseIndex",
        component: () => import("../views/house/index.vue"),
        meta: { title: "仓库管理", icon: "HomeFilled" },
      },
    ],
  },
  {
    path: "/finance",
    name: "ProductData",
    component: Layout,
    children: [
      {
        path: "status",
        name: "OperationStatus",
        component: () => import("../views/OperationData/OperationStatus.vue"),
      },
      {
        path: "cost-analysis",
        name: "CostAnalysis",
        component: () => import("../views/OperationData/CostAnalysis.vue"),
      },
    ],
  },
  {
    path: "/system/user-management",
    component: Layout,
    children: [
      {
        path: "",
        name: "UserList",
        component: () => import("../views/user/UserList.vue"),
      },
    ],
  },
  {
    path: "/finance",
    name: "ProductData",
    component: Layout,
    children: [
      {
        path: "accounting",
        name: "FinanceAccounting",
        component: () => import("../views/finance/Accounting.vue"),
      },
    ],
  },
  {
    path: "/inventory",
    component: Layout,
    children: [
      {
        path: "list",
        component: () => import("../views/inventory/List.vue"),
      },
    ],
  },
  {
    path: "/procurement",
    component: Layout,
    name: "Procurement",
    children: [
      {
        path: "order",
        name: "ProcurementList",
        component: () => import("../views/order/index.vue"),
      },
    ],
  },
  {
    path: "/inventoryin",
    component: Layout,
    name: "inventoryin",
    children: [
      {
        path: "list",
        name: "inventoryinList",
        component: () => import("../views/inventoryin/List.vue"),
      },
    ],
  },
  {
    path: "/operation/stall",
    component: Layout,
    children: [
      {
        path: "",
        name: "StallManagement",
        component: () => import("../views/log/logs.vue"),
      },
    ],
  },
  {
    path: "/system/log",
    component: Layout,
    children: [
      {
        path: "",
        name: "SystemLog",
        component: () => import("../views/SystemLog/Syslog.vue"),
      },
    ],
  },
  {
    path: "/inventoryout",
    component: Layout,
    name: "inventoryout",
    children: [
      {
        path: "list",
        name: "inventoryoutList",
        component: () => import("../views/inventoryout/List.vue"),
      },
    ],
  },
  {
    path: "/echarts",
    path: "/echarts",
    component: Layout,
    children: [
      {
        path: "",
        name: "Echarts",
        component: () => import("../views/Echarts/Index.vue"),
      },
    ],
  },
  {
    path: "/system/user-setting",
    component: Layout,
    children: [
      {
        path: "",
        name: "SysCfg",
        component: () => import("../views/SysCfg/settings.vue"),
      },
    ],
  },
  // 404
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: () => import("../views/NotFound.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 添加路由守卫: 判断用户是否登录，没登录则跳转登录页面
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore();
  const extraUrls = ["/", "/login"];

  if (
    extraUrls.includes(to.path) ||
    to.name === "NotFound" ||
    authStore.isAuthenticated
  ) {
    next();
  } else {
    next("/login");
  }

  // if (to.path !== '/' && !authStore.isAuthenticated) {
  //   next('/')
  // } else {
  //   next()
  // }
  // next()
});

export default router;
